package com.portal.sso.whitelist.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.portal.sso.core.config.PortalResault;
import com.portal.sso.core.model.ResponseTModel;
import com.portal.sso.core.model.WhitelistModel;
import com.portal.sso.core.util.PortalSecureUtil;
import com.portal.sso.whitelist.model.RespointModel;
import com.portal.sso.whitelist.server.WhitelistModelServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: portal-sso
 * @description: 后台管理控制器
 * @author: xuduo
 * @create: 2021-05-07 20:29
 * @Version: 1.0
 **/
@Controller
@RequestMapping("/admin")
public class AdminController {
    //    @PreAuthorize("hasAnyRole('admin')")
    @Autowired
    WhitelistModelServer whitelistModelServer;

    @RequestMapping("/secret")
    @ResponseBody
    public String GetSecret() {
        return PortalSecureUtil.GetHex16Key();
    }

    @GetMapping("/secret-page")
    public String SecretPage() {
        return "admin/secret";
    }

    @GetMapping("login")
    public String adminLogin(Model model, HttpServletRequest request, HttpServletResponse response) {
        String uuid = IdUtil.simpleUUID();
        model.addAttribute("code_key_url", request.getContextPath() + "/code?code_key=" + uuid);
        model.addAttribute("code_key", uuid);
        return "admin/login";
    }

    @PreAuthorize("hasAnyRole('admin')")
    @GetMapping("/index")
    public String index() {
        return "admin/index";
    }

    @PostMapping("/loginon")
    @ResponseBody
    public String adminLoginOn() {
        return "ok";
    }

    @GetMapping("/client-list")
    public String clientList() {
        return "admin/client-list";
    }

    @PostMapping("/client-list-json")
    @ResponseBody
    public String clientListJson() {
        return whitelistModelServer.getListAllJson();
    }

    @GetMapping("/client-key")
    public String clientKey() {
        return "admin/client-key";
    }

    @PostMapping("/add")
    @ResponseBody
    public String addItem(@RequestBody WhitelistModel item) {
        if (item != null) {
            if (whitelistModelServer.isExist(item.getAppId())) {
                //修改
                whitelistModelServer.EditItem(item);
            } else {
                //新增
                whitelistModelServer.AddItem(item);
            }

            return JSONUtil.toJsonStr(new ResponseTModel(PortalResault.OK));

        }
        return JSONUtil.toJsonStr(new ResponseTModel(PortalResault.ERR_FAILURE));
    }

    @PostMapping("/getKey")
    @ResponseBody
    public String getKeyByAppid(String appId) {
        if (StrUtil.isNotEmpty(appId)) {
           return whitelistModelServer.getKey(appId);
        }
        return "";
    }
    @PostMapping("/del")
    @ResponseBody
    public String delClient(String appId){
        try {
            if(StrUtil.isNotEmpty(appId)){
                whitelistModelServer.DelItem(appId);
                return JSONUtil.toJsonStr(new ResponseTModel(PortalResault.OK));
            }
        }catch (Exception e){

        }
        return JSONUtil.toJsonStr(new ResponseTModel(PortalResault.ERR_FAILURE));
    }
}
